<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class News extends Access_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('news_model');
		$this->load->model('crud_model');
	}

	function index()
	{
		$page["_contents"] = "administrator/news/v_news";

		//$data["query"] = $this->users_model->getAll();

		$this->template->load("administrator/template/main_template",$page);
	}

	function load_list()
	{
		
		$arr['data'] = array();
		$query = $this->news_model->getAll();
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			$created = "<span class='username-tbl-info'>" . $row->username . "</span>";
			$created .= "<br><span class='time-tbl-infp'>" . $row->time_created . "</span>";
			if($row->status == '1')
			{
				$status = '<span class="label label-sm label-success">Published</span>';
			}
			else
			{
				$status = '<span class="label label-sm label-warning">Unpublished</span>';
			}
			
			array_push($data,$row->title);
			array_push($data,$created);
			array_push($data,$status);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		echo json_encode($arr);
	}

	function add()
	{
		if($_FILES['image']['size'] == 0)
		{
			$data["id"] = "";
			$data["user_id"] = $this->session->userdata("user_id");
			$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
			$data["title"] = $this->input->post("title");
			$data["content"] = $this->input->post("content");
			$data["status"] = $this->input->post("status");
		}
		else
		{
			$config['upload_path'] = './uploads/news';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '3000';
			$config['max_height']  = '3000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				
				$upload = $this->upload->data();

				$data["id"] = "";
				$data["user_id"] = $this->session->userdata("user_id");
				$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
				$data["title"] = $this->input->post("title");
				$data["content"] = $this->input->post("content");
				$data["status"] = $this->input->post("status");
				$data["image"] = $upload['file_name'];
			}
		}

		$this->crud_model->save('news',$data);
		return true;
	}

	function edit()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->news_model->get_by_id($id)->row();
		
		$arr = array(array('id' => $id, 
						   'title' => $row->title, 
						   'content' => $row->content, 
						   'status' => $row->status,
						   'image' => $row->image
						   )
					);
		echo json_encode($arr );
		
	}

	function update()
	{
		$id = $this->input->post("id");
		$row = $this->news_model->get_by_id($id)->row();

		if($_FILES['image']['size'] == 0)
		{
			
			$data["user_id"] = $this->session->userdata('user_id');
			$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
			$data["title"] = $this->input->post("title");
			$data["content"] = $this->input->post("content");
			$data["status"] = $this->input->post("status");

		}
		else
		{

			$config['upload_path'] = './uploads/news';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '3000';
			$config['max_height']  = '3000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				unlink('./uploads/news/'.$row->image);

				$upload = $this->upload->data();

				$data["user_id"] = $this->session->userdata('user_id');
				$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
				$data["title"] = $this->input->post("title");
				$data["content"] = $this->input->post("content");
				$data["status"] = $this->input->post("status");
				$data["image"] = $upload['file_name'];

			}
		}		

		
		
		$this->crud_model->update('news', $id, $data, 'id');
		return true;
	}
	
	function delete()
	{
		$id = $this->input->post('id');
		$row = $this->news_model->get_by_id($id)->row();

		$this->crud_model->delete('news', $id, 'id');

		unlink('./uploads/news/'.$row->image);
		return true;
	}

}

/* End of file news.php */
/* Location: ./application/controllers/administrator/news.php */